package com.next.muscu.data;

import java.beans.PropertyChangeListener;
import java.beans.PropertyChangeSupport;
import java.text.DateFormat;
import java.text.ParseException;
import java.util.Date;
import javax.persistence.Basic;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.NamedQueries;
import javax.persistence.NamedQuery;
import javax.persistence.Table;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;
import javax.persistence.Transient;

@Entity
@Table(name = "photo", catalog = "jaydb", schema = "")
@NamedQueries({
    @NamedQuery(name = "Photo.findAll", query = "SELECT p FROM Photo p"),
    @NamedQuery(name = "Photo.findById", query = "SELECT p FROM Photo p WHERE p.id = :id"),
    @NamedQuery(name = "Photo.findByDescription", query = "SELECT p FROM Photo p WHERE p.description = :description"),
    @NamedQuery(name = "Photo.findByPhotoPath", query = "SELECT p FROM Photo p WHERE p.photoPath = :photoPath"),
    @NamedQuery(name = "Photo.findByDatePhoto", query = "SELECT p FROM Photo p WHERE p.datePhoto = :datePhoto"),
    @NamedQuery(name = "Photo.findByIdUser", query = "SELECT p FROM Photo p WHERE p.idUser = :idUser")})
public class Photo implements MyDataObject , java.io.Serializable {

	public static final String PP_ID = "id";
	public static final String PP_ID_USER = "idUser";
	public static final String PP_PHOTO_PATH = "photoPath";
	public static final String PP_Code = "datePhoto";
	public static final String PP_DESCRIPTION = "description";
	
        @Id
        @GeneratedValue(strategy = GenerationType.IDENTITY)
        @Basic(optional = false)
	@Column(name = "ID")
        private Integer id;
	@Basic(optional = false)  
        @Column(name = "DESCRIPTION",length = 128)
        private String description = new String("");
        @Column(name = "PHOTO_PATH",length = 128)
	private String photoPath = new String("");
        @Column(name = "DATE_PHOTO")
        @Temporal(TemporalType.DATE)
	private Date datePhoto = new Date();
	@Basic(optional = false)
        @Column(name = "ID_USER")
        private int idUser = new Integer(1);
	
        @Transient
        PropertyChangeSupport ppChangeSupport = new PropertyChangeSupport(this);
	
	public Photo() {
	}

	public Photo(String description, int idUser) {
		this.description = description;
		this.idUser = idUser;
	}

	public Photo(String description, String photoPath, Date datePhoto,
			int idUser) {
		this.description = description;
		this.photoPath = photoPath;
		this.datePhoto = datePhoto;
		this.idUser = idUser;
	}

	public Integer getId() {
		return this.id;
	}

	public void setId(Integer id) {
		this.id = id;
	}

	public String getDescription() {
		return this.description;
	}

	public void setDescription(String description) {
		this.description = description;
	}

	public String getPhotoPath() {
		return this.photoPath;
	}

	public void setPhotoPath(String photoPath) {
		this.photoPath = photoPath;
	}

	public Date getDatePhoto() {
		return this.datePhoto;
	}

	public void setDatePhoto(Date datePhoto) {
		this.datePhoto = datePhoto;
	}

	public int getIdUser() {
		return this.idUser;
	}

	public void setIdUser(int idUser) {
		this.idUser = idUser;
	}

	@Override
	public boolean copy(MyDataObject o) {
		boolean retour = false;
		if (!(o instanceof Photo))
		{
			Photo p = (Photo) o;
			this.id = o.getId();
			this.description = p.getDescription();
			this.photoPath = p.getPhotoPath();
			this.datePhoto = p.getDatePhoto();
			this.idUser = p.getIdUser();
			retour = true;
		}
		return retour;
	}

	@Override
	public void Create() {
		try {
			DateFormat.getDateInstance().parse("01/01/1900");
		} catch (ParseException e) {
			datePhoto = new Date(1);
			e.printStackTrace();
		}
	}



	@Override
	 public boolean equals(Object o) {
	        if (!(o instanceof Photo))
	            return false;
	        Photo e = (Photo)o;
	        return e.getId().equals(id);
	    }
	@Override
	    public int hashCode() {
	        return id.hashCode();
	    }

	    public int compareTo(Photo e) {
	        return this.getId().compareTo(e.getId());
	    }
		public void addPropertyChangeListener(PropertyChangeListener listener) {
			ppChangeSupport.addPropertyChangeListener(listener);
		}
		public void addPropertyChangeListener(String propertyName,
				PropertyChangeListener listener) {
			ppChangeSupport.addPropertyChangeListener(propertyName, listener);
		}
		public PropertyChangeListener[] getPropertyChangeListeners() {
			return ppChangeSupport.getPropertyChangeListeners();
		}
		public PropertyChangeListener[] getPropertyChangeListeners(
				String propertyName) {
			return ppChangeSupport.getPropertyChangeListeners(propertyName);
		}
		public void removePropertyChangeListener(PropertyChangeListener listener) {
			ppChangeSupport.removePropertyChangeListener(listener);
		}
		public void removePropertyChangeListener(String propertyName,
				PropertyChangeListener listener) {
			ppChangeSupport
					.removePropertyChangeListener(propertyName, listener);
		}
}
